package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.WmsUser;
import com.qf.service.WmsUserService;
import com.qf.until.R;
import com.qf.until.RsaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * --- 天道酬勤 ---
 *
 * @author Xiaoze
 * @date 2024/7/22
 * @desc
 */
@RestController
@RequestMapping("/wms/user")
public class WmsUserController {
    @Autowired
    private WmsUserService wmsUserService;
    @PostMapping("/login")
    public R login(@RequestBody WmsUser wmsUser , HttpSession session) throws Exception {
        System.out.println("login接收的数据"+wmsUser);
        // 对密码解析
       String pwd = RsaUtils.decryptByPrivateKey(wmsUser.getPassword());
       wmsUser.setPassword(pwd);
        WmsUser user = wmsUserService.login(wmsUser);
        if(user != null ){
            session.setAttribute("userInfo",user);
            // 造假token
            HashMap<String, String> map = new HashMap<>();
            map.put("token", UUID.randomUUID().toString());
            return R.ok(map);
        }
        return R.fail();
    }
    @GetMapping("info")
    public R getInfo(HttpSession session) {
        WmsUser info = (WmsUser) session.getAttribute("userInfo");
        System.out.println("SysUserController.getUserInfo:"+info);
        info.setAvatar("https://xiaoze-taitai.oss-cn-beijing.aliyuncs.com/"+info.getAvatar());
        return R.ok(info);
    }
    @PostMapping("/logout")
    public R logout(HttpSession session) {
        session.invalidate();
        return R.ok();
    }

    //查询全部的用户信息
    //分页 模糊
    @GetMapping("/findAll")
    public R findAll(@RequestParam HashMap<String, String> map) {
        int pageNum = Integer.parseInt(map.get("pageNum"));
        int pageSize = Integer.parseInt(map.get("pageSize"));
        PageHelper.startPage(pageNum, pageSize);
        List<WmsUser> list = wmsUserService.findAll(map);
        PageInfo<WmsUser> info = new PageInfo<>(list);
        return R.ok(info);
    }

    /**
     * 批量删除
     */
    @GetMapping("/delBatch")
    public R delBatch(@RequestParam List<Integer> ids) {
        System.out.println("批量删除从前端传回来的数据" + ids);
        boolean isok = wmsUserService.delBatch(ids);
        return R.ok(isok);
    }
    /**
     * 添加
     */
    @PostMapping("/add")
    public R addWmsUser(@RequestBody  WmsUser wmsUser) {
        boolean isok  = wmsUserService.addWmsUser(wmsUser);
        return R.ok(isok);
    }
}
